package com.hsource.doctorcenter.service.version.impl;

import cn.hutool.core.convert.Convert;
import com.hsource.doctorcenter.domain.version.ApkVersion;
import com.hsource.doctorcenter.mapper.ApkVersionMapper;
import com.hsource.doctorcenter.service.version.IApkVersionService;
import com.hsource.doctorcenter.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * apk版本信息Service业务层处理
 *
 * @author yangyang
 * @date 2020-10-28
 */
@Service
public class ApkVersionServiceImpl implements IApkVersionService {
    @Autowired
    private ApkVersionMapper apkVersionMapper;

    /**
     * 查询apk版本信息
     *
     * @param id apk版本信息ID
     * @return apk版本信息
     */
    @Override
    public ApkVersion selectApkVersionById(String id) {
        return apkVersionMapper.selectApkVersionById(id);
    }

    /**
     * 查询apk版本信息列表
     *
     * @param apkVersion apk版本信息
     * @return apk版本信息
     */
    @Override
    public List<ApkVersion> selectApkVersionList(ApkVersion apkVersion) {
        return apkVersionMapper.selectApkVersionList(apkVersion);
    }

    /**
     * 新增apk版本信息
     *
     * @param apkVersion apk版本信息
     * @return 结果
     */
    @Override
    public int insertApkVersion(ApkVersion apkVersion) {
        return apkVersionMapper.insertApkVersion(apkVersion);
    }

    /**
     * 修改apk版本信息
     *
     * @param apkVersion apk版本信息
     * @return 结果
     */
    @Override
    public int updateApkVersion(ApkVersion apkVersion) {
        return apkVersionMapper.updateApkVersion(apkVersion);
    }

    /**
     * 删除apk版本信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteApkVersionByIds(String ids) {
        return apkVersionMapper.deleteApkVersionByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除apk版本信息信息
     *
     * @param id apk版本信息ID
     * @return 结果
     */
    @Override
    public int deleteApkVersionById(String id) {
        return apkVersionMapper.deleteApkVersionById(id);
    }


    /**
     * 获取当前版本信息
     * @param version
     * @param channel
     * @return
     */
    @Override
    public ApkVersion checkVersion(String version, String channel) {
        ApkVersion av=new ApkVersion();
        av.setChannel(Integer.parseInt(channel));
        List<ApkVersion> apkVersions = apkVersionMapper.selectApkVersionList(av);

        if (StringUtils.isNotNull(apkVersions)&&apkVersions.stream().count()>0) {
            ApkVersion apkVersion = apkVersions.stream().findFirst().get();
            String currentVersion = apkVersion.getApkversion();
            if (!version.equals(currentVersion)) {
                apkVersion.setStatus("0");
            } else {
                apkVersion.setStatus("1");
            }
            return apkVersion;
        } else {
           return null;
        }
    }
}
